package config

import (
	"io/ioutil"
	"os"
	"wolongauth/errhelp"
	"wolongauth/global"
	"wolongauth/makeconfig"

	"github.com/dingdinglz/dingtools/dingjson"
	"github.com/dingdinglz/dingtools/dinglog"
)

func IsExists(path string) bool {
	_, err := os.Stat(path)
	return err == nil
}
func Load() {
	if !IsExists("config.json") {
		global.Logger.Info("未检出config.json，正在启动生成程序...")
		makeconfig.Run()
	}
	global.Logger.Info("正在加载配置项...")
	config_data_b, err := ioutil.ReadFile("config.json")
	errhelp.ErrDoWithTagAndAbort(err, "config read")
	j := dingjson.NewFromBytes(config_data_b)
	err = j.ConventToStruct(&config_data)
	errhelp.ErrDoWithTagAndAbort(err, "config json paser")
	if config_data.Debug {
		global.Logger.SetLevel(dinglog.Level_Debug)
	}
	global.Logger.Debug(config_data)
}

func GetConfig() ConfigModel {
	return config_data
}
